package com.wisewin.circle.util.wechat.xcx.util;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class JsonResponseHandler {
    public JsonResponseHandler() {
    }

    public static <T> ResponseHandler<T> createResponseHandler(Class<T> clazz) {
        return new JsonResponseHandlerImpl((String) null, clazz);
    }

    public static class JsonResponseHandlerImpl<T> extends LocalResponseHandler implements ResponseHandler<T> {
        private Class<T> clazz;

        public JsonResponseHandlerImpl(String uriId, Class<T> clazz) {
            this.uriId = uriId;
            this.clazz = clazz;
        }

        public T handleResponse(HttpResponse response) throws ClientProtocolException, IOException {
            HttpEntity entity = response.getEntity();
            String str = EntityUtils.toString(entity, "utf-8");
            return JsonUtil.parseObject(str, this.clazz);
        }
    }
}
